package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.TjbbService;

import net.sf.json.JSONObject;
/**  
 * @类名 TjbbController  
 * @功能 TODO  财务报表统计类
 * @作者 js 
 * @日期 2016年12月7日 上午9:55:42  
 *    
 */
@Controller
@RequestMapping("/tjbb")
public class TjbbController extends JsonController{
	@Autowired
	private TjbbService tjbbService;
	/**
	 * @作者 js  
	 * @方法名 queryTjbbDay  
	 * @功能 TODO 日报统计接口
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryTjbbDay")
	public ModelAndView queryTjbbDay(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		List<Map<String, Object>> list=new ArrayList<>();
		Map<String, Object> map=new HashMap<>();
		String tjDate = RequestUtils.getStringParameter(request, "tjDate");//当前统计日期
		list=tjbbService.queryTjbbDayList(tjDate);//获取各公司,门店分项数据
		map=tjbbService.getSumTjbbDay(tjDate);//获取总数据
		result.put("data", list);
		result.put("sumJe", map);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 qjy  
	 * @方法名 queryTjbbDayReport  
	 * @功能 TODO 日报统计图形报表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping("/queryTjbbDayReport")
	public ModelAndView queryTjbbDayReport(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject params = new JSONObject();
		String startDate= RequestUtils.getStringParameter(request, "startDate");
		String endDate= RequestUtils.getStringParameter(request, "endDate");
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		List<Map<String, Object>> list = tjbbService.queryTjbbDayReport(params);
		serialize(request,response,list);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 exportTjbbDayExcelMethod  
	 * @功能 TODO 日报Excel下载接口
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("exportTjbbDayExcelMethod")
	public ModelAndView exportTjbbDayExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String tjDate = RequestUtils.getStringParameter(request, "tjDate");
		// 查询数据
		List<Map<String, Object>> list = tjbbService.queryTjbbDayList(tjDate);
		Map<String, Object> map=tjbbService.getSumTjbbDay(tjDate);
		map.put("GSMC", "合计:");
		list.add(map);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.exportTjbbDayExcelMethod(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(tjDate+"日报表");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = tjDate + "财务日报表.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
	
	/**
	 * @作者 js  
	 * @方法名 queryTjbbMonth  
	 * @功能 TODO 月报统计接口
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryTjbbMonth")
	public ModelAndView queryTjbbMonth(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		List<Map<String, Object>> list=new ArrayList<>();
		Map<String, Object> map=new HashMap<>();
		String tjym = RequestUtils.getStringParameter(request, "tjym");//当前统计年月
		list=tjbbService.queryTjbbMonthList(tjym);//获取各公司,门店分项数据
		map=tjbbService.getSumTjbbMonth(tjym);//获取总数据
		result.put("data", list);
		result.put("sumJe", map);
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("exportTjbbMonthExcelMethod")
	public ModelAndView exportTjbbMonthExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String tjym = RequestUtils.getStringParameter(request, "tjym");
		// 查询数据
		List<Map<String, Object>> list = tjbbService.queryTjbbMonthList(tjym);
		Map<String, Object> map=tjbbService.getSumTjbbMonth(tjym);
		map.put("GSMC", "合计:");
		list.add(map);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.exportTjbbMonthExcelMethod(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(tjym+"月报表");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = tjym + "财务月报表.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 queryTjbbDayListByOa  
	 * @功能 TODO 查看分公司,门店具体用户数据
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryTjbbDayListByOa")
	public ModelAndView queryTjbbDayListByOa(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params=new JSONObject();
		int start=RequestUtils.getIntParameter(request, "start");
		int limit=RequestUtils.getIntParameter(request, "limit");
		String tjDate = RequestUtils.getStringParameter(request, "tjDate");//当前统计日期
		String gsmc = RequestUtils.getStringParameter(request, "gsmc");//公司名称
		String mdmc = RequestUtils.getStringParameter(request, "mdmc");//门店名称
		String username = RequestUtils.getStringParameter(request, "username");//门店名称
		params.put("start", start);
		params.put("limit", limit);
		params.put("tjDate", tjDate);
		params.put("gsmc", gsmc);
		params.put("mdmc", mdmc);
		params.put("username", username);
		List<Map<String, Object>> list=tjbbService.queryTjbbDayListByOa(params);
		int count=tjbbService.getTjbbDayListByOaCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/queryTjbbMonthListByOa")
	public ModelAndView queryTjbbMonthListByOa(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params=new JSONObject();
		int start=RequestUtils.getIntParameter(request, "start");
		int limit=RequestUtils.getIntParameter(request, "limit");
		String tjym = RequestUtils.getStringParameter(request, "tjym");//当前统计日期
		String gsmc = RequestUtils.getStringParameter(request, "gsmc");//公司名称
		String mdmc = RequestUtils.getStringParameter(request, "mdmc");//门店名称
		String username = RequestUtils.getStringParameter(request, "username");//门店名称
		params.put("start", start);
		params.put("limit", limit);
		params.put("tjym", tjym);
		params.put("gsmc", gsmc);
		params.put("mdmc", mdmc);
		params.put("username", username);
		List<Map<String, Object>> list=tjbbService.queryTjbbMonthListByOa(params);
		int count=tjbbService.getTjbbMonthListByOaCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 queryTjbbZzMap  
	 * @功能 TODO 平台总账报表查询
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryTjbbZzMap")
	public ModelAndView queryTjbbZzMap(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		Map<String, Object> data=tjbbService.queryTjbbZzMap();
		result.put("data", data);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 queryTjbbZzMapSearch  
	 * @功能 TODO 平台总账报表_带查询条件
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryTjbbZzMapSearch")
	public ModelAndView queryTjbbZzMapSearch(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String startDate=RequestUtils.getStringParameter(request, "startDate");
		String endDate=RequestUtils.getStringParameter(request, "endDate");
		JSONObject result = new JSONObject();
		Map<String, Object> data=tjbbService.queryTjbbZzMapSearch(startDate, endDate);
		result.put("data", data);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 querySyAndBjList  
	 * @功能 TODO 查询某一天发放的收益或者本金明细
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querySyAndBjList")
	public ModelAndView querySyAndBjList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		List<Map<String, Object>> list=new ArrayList<>();
		Integer start=RequestUtils.getIntParameter(request, "start");
		Integer limit=RequestUtils.getIntParameter(request, "limit");
		String tjDate = RequestUtils.getStringParameter(request, "tjDate");//当前统计日期
		String type = RequestUtils.getStringParameter(request, "type");//当前统计日期
		
		list=tjbbService.querySyAndBjList(type, tjDate, start, limit);
		int count=tjbbService.getSyAndBjCount(type, tjDate);
		String sumJe=tjbbService.getSyAndBjSum(type, tjDate);
		result.put("data", list);
		result.put("total", count);
		result.put("sumJe", sumJe);
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/getSyAndBjSumJe")
	public ModelAndView getSyAndBjSumJe(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		String tjDate = RequestUtils.getStringParameter(request, "tjDate");//当前统计日期
		String sumJe_sy=tjbbService.getSyAndBjSum("0x01", tjDate);
		String sumJe_bj=tjbbService.getSyAndBjSum("0x02", tjDate);
		result.put("sumJe_sy", sumJe_sy);
		result.put("sumJe_bj", sumJe_bj);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 queryZzMap  
	 * @功能 TODO 平台总账报表-新版
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryZzMap")
	public ModelAndView queryZzMap(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		Map<String, String> data=tjbbService.getZzMap(null, null);
		result.put("data", data);
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/listSysSy")
	public ModelAndView listSysSy(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		List<Map<String, Object>> list=new ArrayList<>();
		Integer start=RequestUtils.getIntParameter(request, "start");
		Integer limit=RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String sylx = RequestUtils.getStringParameter(request, "sylx");
		JSONObject params=new JSONObject();
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("sylx", sylx);
		list=tjbbService.listSysSy(params);
		int count=tjbbService.getSysSyCount(params);
		String sumJe=tjbbService.getSysSySumJe(params);
		result.put("data", list);
		result.put("total", count);
		result.put("sumJe", sumJe);
		serialize(request, response, result);
		return null;
	}
}


